package com.steven.ourcontest.contest.view

import android.os.Bundle
import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import com.steven.ourcontest.R
import com.steven.ourcontest.app.AppRouter
import com.steven.ourcontest.base.BaseActivity
import com.steven.ourcontest.contest.adapter.MatchTeamAdapter
import com.steven.ourcontest.contest.viewmodel.MatchDetailViewModel
import com.steven.ourcontest.utils.DateUtil
import com.steven.ourcontest.utils.toast
import kotlinx.android.synthetic.main.activity_match_detail.*
import kotlinx.android.synthetic.main.activity_match_detail.iv_collect
import kotlinx.android.synthetic.main.activity_match_detail.tv_belong
import kotlinx.android.synthetic.main.activity_match_detail.tv_match_time
import kotlinx.android.synthetic.main.activity_match_detail.tv_rank
import kotlinx.android.synthetic.main.activity_match_detail.tv_signup_time
import kotlinx.android.synthetic.main.activity_match_detail.tv_url
import kotlinx.android.synthetic.main.activity_team_detail.*

class MatchDetailActivity : BaseActivity() {
    private var matchId = 0L
    private val matchDetailViewModel: MatchDetailViewModel by viewModels()
    private val adapter by lazy {
        MatchTeamAdapter(this) {
            matchDetailViewModel.applyToTeam(it, {
                toast("申请成功！")
            }, { error ->
                toast(error)
            })
        }
    }
    private var isCollected: Boolean? = null
    override fun getStatusBarColor(): Int {
        return R.color.white
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun getViewId(): Int? {
        return R.layout.activity_match_detail
    }

    override fun initView() {
        matchId = intent.getLongExtra("matchId", 0)
        if (matchId == 0L) {
            finish()
            return
        }
        rv_team?.adapter = adapter
        matchDetailViewModel.matchRes.observe(this, Observer {
            if (it != null) {
                val data = it.data.firstOrNull()
                if (data != null) {
                    tv_match_name?.text = data.name
                    data.belong?.let {
                        tv_belong?.text = "主办方：${it}"
                    }
                    data.rank?.let {
                        tv_rank?.text = "级别：${it}"
                    }
                    tv_signup_time?.text = "报名时间：${if (data.signUpStartTime != 0L && data.signUpEndTime != 0L) {
                        DateUtil.timeStamp2Date(
                            data.signUpStartTime?.times(1000),
                            "yyyy.MM.dd"
                        ) + "~" + DateUtil.timeStamp2Date(data.signUpEndTime?.times(1000), "yyyy.MM.dd")
                    } else {
                        "暂无"
                    }}"
                    tv_match_time?.text = "比赛时间：${if (data.matchStartTime != 0L && data.matchEndTime != 0L) {
                        DateUtil.timeStamp2Date(
                            data.matchStartTime?.times(1000),
                            "yyyy.MM.dd"
                        ) + "~" + DateUtil.timeStamp2Date(data.matchEndTime?.times(1000), "yyyy.MM.dd")
                    } else {
                        "暂无"
                    }}"
                    tv_url?.text = "参考网址：${data.url}"
                    tv_url?.setOnClickListener {
                        data.url?.let { it1 -> AppRouter.openWeb(this, it1) }
                    }
                    if (it.isCollected) {
                        iv_collect?.setImageResource(R.drawable.ic_collected)
                    } else {
                        iv_collect?.setImageResource(R.drawable.ic_uncollected)
                    }
                    isCollected = it.isCollected
                }
            }
        })
        iv_collect?.setOnClickListener {
            if (isCollected == true) {
                matchDetailViewModel.deleteMatchCollection(matchId) {
                    iv_collect?.setImageResource(R.drawable.ic_uncollected)
                    isCollected = false
                }
            } else if (isCollected == false) {
                matchDetailViewModel.addMatchCollection(matchId) {
                    iv_collect?.setImageResource(R.drawable.ic_collected)
                    isCollected = true
                }
            }
        }
        matchDetailViewModel.teamRes.observe(this, Observer {
            if (it != null) {
                if (it.data.isEmpty()) {
                    tv_team_title?.isVisible = false
                    rv_team?.isVisible = false
                    tv_tips?.isVisible = true
                    tv_tips?.setOnClickListener {
                        AppRouter.startAddTeamActivity(this)
                    }
                } else {
                    tv_team_title?.isVisible = true
                    rv_team?.isVisible = true
                    tv_tips?.isVisible = false
                    adapter.listData.clear()
                    adapter.listData.addAll(it.data)
                    adapter.notifyDataSetChanged()
                }
            } else {
                tv_team_title?.isVisible = false
                rv_team?.isVisible = false
            }
        })
        matchDetailViewModel.queryMatchDetail(matchId)
        matchDetailViewModel.searchTeamByMatch(matchId)
        matchDetailViewModel.addHistory(matchId)
    }
}